home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 April: Mac OS SDK / Dev.CD Apr 00 SDK1.toast / Development Kits / Mac OS / Translation Manager / Sample Code / Development Shell (old) / THINKTranslationAppShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-05  |  9.3 KB  |  339 lines  |  [TEXT/KAHL]

  1. /*
  2.     File:        THINKTranslationAppShell.c 
  3.  
  4.     Contains:    Application shell for creating a Translation Extension in THINK C.
  5.     
  6.     Copyright:    © 1992 by Apple Computer, Inc., all rights reserved.
  7.  
  8.      Overview:
  9.          - register our translation extension as a component
  10.          - create a temp file (in the preferences folder) and get FSSpec
  11.          - call StandardGetFile to obtain an FSSpec for the file to be translated
  12.          - call ExecuteTranslation with these two FSSpecs
  13.          - voila! the translation is executed via our translation extension calls
  14.              (debuggable in Think C!)
  15.  
  16.      What you have to do to use it:
  17.          - add your translation extension source to the project and compile
  18.         - modify the 'thng' resource in THINKTranslationAppShell.π.rsrc to have your manufacturer signature
  19.          - add your translation extension's resources to THINKTranslationAppShell.π.rsrc
  20.          - to do a file translation:
  21.                  - select a file to translate
  22.                  - select a destination format
  23.                  - run the translation, you will be able to debug your file
  24.                      translation routines in the Think C debugger
  25.          - to do a scrap translation (to 'styl' only supported now)
  26.                  - twitch to source app, choose Copy
  27.                  - twitch to this app, choose "Do 'styl' Translation"
  28.                  - twitch to destination app, choose Paste
  29.  
  30. */
  31.  
  32. #ifndef THINK_C
  33. #include <Types.h>
  34. #include <Quickdraw.h>
  35. #include <Scrap.h>
  36. #include <StandardFile.h>
  37. #include <Menus.h>
  38. #include <Resources.h>
  39. #include <ToolUtils.h>
  40. #include <Desk.h>
  41. #include <Fonts.h>
  42. #include <OSEvents.h>
  43. #endif
  44. #include <Folders.h>
  45. #include <Script.h>
  46.  
  47. #include "TranslationExtensions.h"
  48. #include "Components.h"
  49.  
  50.  
  51. #define appleID            128                /* resource IDs of menus */
  52. #define testID            129
  53.  
  54. #define appleM            0                /* menu indices */
  55. #define testM            1
  56.  
  57. #define setSourceCmd        1
  58. #define destFormatCmd        2
  59. #define translateFileCmd    3
  60. #define translateScrapCmd    5
  61. #define quitCmd                7
  62.  
  63.  
  64.  
  65. //////////////////////////////////////////////////////////////////////////////////////////////
  66. //
  67. //  ExecuteTranslation
  68. //
  69. // This routine allows the developer to use THINK C to debug a File Translation Extension.
  70. // It executes something very similar to a desktop translator.
  71. //
  72. // Enter:    theDocument            If theDocument is NULL, this routine will display and
  73. //                                run the Desktop Translator dialog (storing the setting in the
  74. //                                preference file (described next).  If theDocument is not NUILL, 
  75. //                                then theDocument will be translated into the format specified in
  76. //                                the preference file.  The new document will be created in the
  77. //                                same directory as theDocument.
  78. //            thePreferenceFile    An FSSpec describing where and what the name of the preference
  79. //                                file.  The file should already been created (and have a resource
  80. //                                fork) prior to making this call.  It's also important that the
  81. //                                preference file is closed prior to this call.
  82. //
  83. // Exit:    returns                Any errors that might occur
  84. //
  85. // ••• IMPORTANT : DISCLAMER •••
  86. //
  87. // THE TRAP EXECUTETRANSLATION() IS NOT FULLY SUPPORTED BY APPLE COMPUTER, INC.  THIS TRAP
  88. // IS PROVIDED ON A "TEMPORARY" BASIS AND WILL BE REMOVED IN THE FUTURE.  YOU SHOULD THEREFORE
  89. // NOT USE THIS TRAP IN ANY WAY EXCEPT WITHIN THIS SHELL.
  90. //
  91. pascal OSErr ExecuteTranslation(const FSSpec*    theDocument,
  92.                                  const FSSpec*    thePreferencesFile)
  93.      = {0x7028,0xABFC}; 
  94.  
  95.  
  96. // globals
  97. Component            gComponent;
  98. Boolean                gSourceDefined = false;
  99. FSSpec                 gSourceFile;        // source document to be translated
  100. FSSpec                gPrefFile;            // file containing information on what format to translate to
  101. MenuHandle            gMenus[2];
  102.  
  103.  
  104.  
  105.  
  106. /////////////////////////////////////////////////////////////////////////////////////////
  107. //
  108. // This is the magic glue that is linked to the beginning of a Translation Extenstion
  109. // and interfaces to the ComponentMgr.  When used in the THINK C environment, 
  110. // a pointer to this routine is passed to RegisterComponent.
  111. //
  112. //
  113. pascal ComponentResult TranslateEntry( ComponentParameters *params, Handle storage )
  114. {
  115.     ComponentRoutine    theRtn;
  116.  
  117.     switch (params->what)
  118.     {
  119.         case kComponentOpenSelect:
  120.             {
  121.                 ComponentInstance    self = (ComponentInstance)params->params[0];
  122.                 Handle                h      = NewHandle(sizeof(ComponentInstance));  // allocate storage
  123.                 
  124.                 if ( h != NULL )
  125.                 {
  126.                     (**(ComponentInstance**)h) = self;      // put instance in storage
  127.                     SetComponentInstanceStorage(self, h);
  128.                     return noErr;
  129.                 }
  130.                 else 
  131.                     return MemError();
  132.             };
  133.         case kComponentCloseSelect:    
  134.             {
  135.                 DisposeHandle(storage);
  136.                 return noErr;
  137.             };
  138.         case kComponentCanDoSelect:    
  139.             {
  140.                 short    selector = *(short*)params->params;
  141.  
  142.                 return ( ((kComponentVersionSelect           <= selector) && (selector <= kComponentOpenSelect))
  143.                       || ((kTranslateGetFileTranslationList  <= selector) && (selector <= kTranslateTranslateFile))
  144.                       || ((kTranslateGetScrapTranslationList <= selector) && (selector <= kTranslateTranslateScrap)) );
  145.             };
  146.         case kComponentVersionSelect:            
  147.                 return noErr;
  148.                 
  149.         case kTranslateGetFileTranslationList:    theRtn = (ComponentRoutine) DoGetFileTranslationList;    break;
  150.         case kTranslateIdentifyFile:            theRtn = (ComponentRoutine) DoIdentifyFile;                break;
  151.         case kTranslateTranslateFile:            theRtn = (ComponentRoutine) DoTranslateFile;            break;
  152.         
  153.         case kTranslateGetScrapTranslationList:    theRtn = (ComponentRoutine) DoGetScrapTranslationList;    break;
  154.         case kTranslateIdentifyScrap:            theRtn = (ComponentRoutine) DoIdentifyScrap;            break;
  155.         case kTranslateTranslateScrap:            theRtn = (ComponentRoutine) DoTranslateScrap;            break;
  156.         default:                                return (badComponentSelector);
  157.     };
  158.     return ( CallComponentFunctionWithStorage((Handle)**(ComponentInstance**)storage, params, theRtn) );
  159. }
  160.  
  161.  
  162. OSErr DoCommand(long mResult)
  163. {
  164.     OSErr                err;
  165.     int                    theItem;
  166.     Str255                name;
  167.     StandardFileReply    theReply;
  168.  
  169.     
  170.     theItem = LoWord(mResult);
  171.     switch (HiWord(mResult)) {
  172.         case appleID:
  173.             GetItem(gMenus[appleM], theItem, &name);
  174.             OpenDeskAcc(name);
  175.             break;
  176.  
  177.         case testID: 
  178.             switch (theItem) {
  179.             
  180.                 // specify the file to be translated
  181.                 case setSourceCmd:                    
  182.                     StandardGetFile(NULL, -1, NULL, &theReply);
  183.                     if (theReply.sfGood)
  184.                     {
  185.                         gSourceFile = theReply.sfFile;
  186.                         gSourceDefined = true;
  187.                     }
  188.                     break;
  189.  
  190.                 // specify the desired type of the output file
  191.                 case destFormatCmd:
  192.                     if ((err = ExecuteTranslation(0, &gPrefFile)) != noErr)
  193.                             return err;
  194.                     break;
  195.                     
  196.                 case translateFileCmd:
  197.                     // if there is a source file specified, translate normally 
  198.                     if (gSourceDefined)
  199.                     {
  200.                         if ((err = ExecuteTranslation(&gSourceFile, &gPrefFile)) != noErr)
  201.                             return err;
  202.                     }
  203.                     break;
  204.                 
  205.                 case translateScrapCmd:
  206.                     {
  207.                     long offset;
  208.                     
  209.                     GetScrap(NULL, 'styl', &offset);
  210.                     break;
  211.                     }
  212.                     
  213.                 case quitCmd:
  214.                     return -1;
  215.             }
  216.     }
  217.     HiliteMenu(0);
  218.     return (noErr);
  219. }
  220.  
  221.  
  222. OSErr DoMouseDown (int windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  223. {
  224.     switch (windowPart) {
  225.         case inMenuBar:
  226.         {
  227.             if (gSourceDefined)
  228.                 EnableItem(gMenus[testM],translateFileCmd);
  229.             else
  230.                 DisableItem(gMenus[testM],translateFileCmd);
  231.             return(DoCommand(MenuSelect(myEvent->where)));
  232.         }
  233.         case inSysWindow:
  234.             SystemClick(myEvent, whichWindow);
  235.             break;
  236.     }
  237. }
  238.  
  239.  
  240. void SetUpPreferenceFile(void)
  241. {    
  242.     // get pref file name
  243.     FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &gPrefFile.vRefNum, &gPrefFile.parID);
  244.     BlockMove("\pTHINKTransShell Pref", gPrefFile.name, 40); 
  245.     
  246.     // create it (it may already exist, but that is OK)
  247.     FSpCreateResFile(&gPrefFile, 'tran', '????', smSystemScript);
  248. }
  249.  
  250.  
  251. void SetUpMenus(void)
  252. {
  253.     int        i;
  254.         
  255.     gMenus[appleM] = NewMenu(appleID, "\p\024");
  256.     AddResMenu(gMenus[appleM], 'DRVR');
  257.     gMenus[testM] = GetMenu(testID);
  258.     for ((i=appleM); (i<=testM); i++)
  259.         InsertMenu(gMenus[i], 0) ;
  260.     DrawMenuBar();
  261. }
  262.  
  263.  
  264. RegisterMyComponent()
  265. {
  266.     ComponentDescription    cd;
  267.     ResourceSpec            nameRes;
  268.     Handle                    componentNameH;
  269.     ComponentResourceHandle    componentThngHdl;
  270.     
  271.     // this application must have a 'thng' resource 128 
  272.     componentThngHdl = (ComponentResourceHandle)GetResource('thng', 128);
  273.     nameRes = (**componentThngHdl).componentName;
  274.     componentNameH = GetResource(nameRes.resType, nameRes.resId);
  275.     
  276.     BlockMove((*componentThngHdl), &cd, sizeof(ComponentDescription)); 
  277.     ReleaseResource((Handle)componentThngHdl);
  278.     
  279.     // Register the component, including its main entry point and its name
  280.     gComponent = RegisterComponent(&cd, &TranslateEntry, false, componentNameH, NULL, NULL);
  281. }
  282.  
  283.  
  284. OSErr MainEvent(void) 
  285. {
  286.     EventRecord        myEvent;
  287.     WindowPtr        whichWindow;
  288.     short            windowPart;
  289.     
  290.     SystemTask();
  291.     if (GetNextEvent(everyEvent, &myEvent)) {
  292.         switch (myEvent.what) {
  293.         case mouseDown:
  294.             windowPart = FindWindow(myEvent.where, &whichWindow);
  295.             return (DoMouseDown(windowPart, whichWindow, &myEvent));
  296.             break;
  297.         case keyDown:
  298.         case autoKey: 
  299.             {
  300.                 register char    theChar;
  301.             
  302.                 theChar = myEvent.message & charCodeMask;
  303.                 if ((myEvent.modifiers & cmdKey) != 0) 
  304.                     {
  305.                     SysBeep(1);
  306.                     return(DoCommand(MenuKey( theChar)));
  307.                     }
  308.                 break;
  309.             }
  310.         } /* end of case myEvent.what */
  311.     } /* if */
  312.     return(noErr);
  313. }
  314.  
  315. main() 
  316. {        
  317. #ifdef THINK_C
  318.     InitGraf(&thePort);                    // the usual initializations...
  319. #else
  320.     InitGraf(&qd.thePort);
  321. #endif
  322.     InitFonts();
  323.     FlushEvents(everyEvent, 0);
  324.     InitWindows();
  325.     InitMenus();
  326.     InitDialogs(0L);
  327.     InitCursor();
  328.     MaxApplZone();
  329.     SetUpMenus();
  330.     
  331.     SetUpPreferenceFile();
  332.     
  333.     RegisterMyComponent();                        
  334.     
  335.         while (MainEvent() == noErr) ;    //         ... go to it
  336.         
  337.     UnregisterComponent(gComponent);
  338. }
  339.